package com.example.annotation;

import org.springframework.stereotype.Service;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.List;

/**
 * @program: springboot_course
 * @description: 自定义注解ListNotHasNull的实现类,用于判断List集合中是否含有null元素
 * @author: ChenZhiXiang
 * @create: 2019-06-11 09:58
 **/
@Service
public class ListNotHasNullValidatorImpl implements ConstraintValidator<ListNotHasNull, List> {

    private int value;

    @Override
    public void initialize(ListNotHasNull constraintAnnotation) {
        // 传入value值，可以在校验中使用
        this.value = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(List value, ConstraintValidatorContext context) {
        if (null != value){
            for (Object o : value){
                if (null == o){
                    //如果List集合中含有Null元素，校验失败
                    return false;
                }
            }
        }
        return true;
    }
}
